﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Xml;
using Thanhlichtek.BOS.Bullhorn;
using Thanhlichtek.BEF.Fix;

namespace Thanhlichtek.BEF.Win
{
    public partial class BullhornAccountsForm : Form
    {
        public BullhornAccountsForm()
        {
            InitializeComponent();
        }

        private String _profileFolder = "Profile";
        private String _fileName = "Profile\\BullhornAccounts.xml";

        private void accountBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            IList accounts = accountBindingSource.List;
            accountBindingSource.EndEdit();
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(List<Account>));
            if (!Directory.Exists(_profileFolder))
            {
                Directory.CreateDirectory(_profileFolder);

            }
            var xw = XmlWriter.Create(_fileName);
            int count = 0;
            List<Account> newBullhornAccounts = new List<Account>();
            foreach (var account in accounts)
            {
                var newBullhornAccount = new Account();
                ((Account)account).Id = count;


                newBullhornAccount.Id = ((Account)account).Id;
                newBullhornAccount.UserName = ((Account)account).UserName;
                newBullhornAccount.Password = CryptoHelper.Encrypt(((Account)account).Password);
                newBullhornAccount.ApiKey = CryptoHelper.Encrypt(((Account)account).ApiKey);
                newBullhornAccounts.Add(newBullhornAccount);
                count++;
            }
            x.Serialize(xw, newBullhornAccounts);
            xw.Close();
            
        }

        private void accountBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (accountBindingSource.List.Count > 0)
            {
                userNameTextBox.Enabled = true;
                passwordTextBox.Enabled = true;
                apiKeyTextBox.Enabled = true;
            }
            else
            {
                userNameTextBox.Enabled = false;
                passwordTextBox.Enabled = false;
                apiKeyTextBox.Enabled = false;
            }
        }

        private void BullhornAccountsForm_Load(object sender, EventArgs e)
        {
            IList accounts = accountBindingSource.List;
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(accounts.GetType());
            if (File.Exists(_fileName))
            {
                XmlReader xr = XmlReader.Create(_fileName);
                accounts = (IList)x.Deserialize(xr);
                foreach (var account in accounts)
                {
                    ((Account)account).Password = CryptoHelper.Decrypt(((Account)account).Password);
                    ((Account)account).ApiKey = CryptoHelper.Decrypt(((Account)account).ApiKey);
                    accountBindingSource.List.Add(account);

                }

                xr.Close();
                
            }
            
        }
    }
}
